package violencerecursion;

/**
 * @author tongchen
 * @create 2023-03-24 16:59
 */
class Solution {
    public static void main(String[] args) {
        int[]nums={2,7,11,15};
        int[] ints = twoSum(nums, 9);
        System.out.println(ints);
    }
    public static int[] twoSum(int[] nums, int target) {
        //利用双指针，一个指针负责顺序遍历，另一个指针负责二分查找
        for(int i=0;i<nums.length;++i){
            int left=i+1;
            int right=nums.length-1;
            while(left<=right){
                int mid=(right-left)>>1+left;
                if(nums[i]+nums[mid]<target){
                    left=mid+1;
                }
                else if(nums[i]+nums[mid]>target){
                    right=mid-1;
                }else{
                    return new int[]{nums[i],nums[mid]};
                }
            }
        }
        return new int[2];

    }
}
